<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog" style="width:600px;">
        <el-form :model="addBaseAddressForm" ref="addBaseAddressRef" :rules="addBaseAddressrules">
            <!-- <el-form-item label="父亲id" :label-width="formLabelWidth" prop="parentId" v-if="addBaseAddressForm.id">
                <el-input v-model="addBaseAddressForm.parentId" autocomplete="off" ref="name"
                    placeholder="请填写父亲id"></el-input>
            </el-form-item> -->
            <el-form-item v-if="this.pName == ''" label="父亲地点名称" :label-width="formLabelWidth" prop="parentName">
                <el-input v-model="addBaseAddressForm.parentName" autocomplete="off" ref="name"
                    placeholder="请填写父亲地点名称"></el-input>
            </el-form-item>
            <el-form-item v-if="this.pName != ''" label="父亲地点名称" :label-width="formLabelWidth" prop="pName">
                <el-input v-model="pName" autocomplete="off" ref="name" placeholder="请填写父亲地点名称"></el-input>
            </el-form-item>
            <el-form-item label="房间号" :label-width="formLabelWidth" prop="name">
                <el-input v-model="addBaseAddressForm.name" autocomplete="off" ref="name"
                    placeholder="请填写房间号"></el-input>
            </el-form-item>
            <el-form-item label="地点名称" :label-width="formLabelWidth" prop="alias">
                <el-input v-model="addBaseAddressForm.alias" autocomplete="off" ref="name"
                    placeholder="请填写地点名称"></el-input>
            </el-form-item>

        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel()">取消</el-button>
                <el-button type="primary" @click="save()">保存</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
    name: "AddBaseAddress",
    components: { ElForm, ElMessage },
    data() {
        return {
            dialogFormVisible: ref(false),
            formLabelWidth: "120px",
            title: "",
            personnelType: "",
            id: '',
            pName: '',
            user: {},
            addBaseAddressForm: {
                id: "",
                code: "",
                name: "",
                alias: "",
                detail: "",
                parentId: "",
                parentName: "",
                sortNum: "",
                createTime: "",
                campus: "",

            },

            // 校验规则
            addBaseAddressrules: {
                // code: vxRule(true, null, "blur"),
                // name: vxRule(true, null, "blur"),
                // alias: vxRule(true, null, "blur"),
                // detail: vxRule(true, null, "blur"),
                parentName: vxRule(true, null, "blur"),
                // parentId: vxRule(true, null, "blur"),
            },
        };
    },
    mounted() {

    },
    methods: {
        closeDialog() {
            this.clear();
        },
        cancel() {
            this.dialogFormVisible = false;
            this.clear();
        },
        getBaseAddressById(id) {
            httpService("/javaApi/baseAddress/queryById", { id: id }, "get").then(
                (data) => {
                    this.addBaseAddressForm = data;
                }
            );
        },
       

         

        clear() {
            this.dialogFormVisible = false;
            //清空form
            this.addBaseAddressForm.id = "";
            this.addBaseAddressForm.code = "";
            this.addBaseAddressForm.name = "";
            this.addBaseAddressForm.alias = "";
            this.addBaseAddressForm.detail = "";
            this.addBaseAddressForm.parentId = "";
            this.addBaseAddressForm.parentName = "";
            this.addBaseAddressForm.sortNum = "";
            this.addBaseAddressForm.createTime = "";
            this.user = "";
            this.pName = "";
            this.id = ""
        },
        async save() {
            const response = await httpService("/javaApi/baseAddress/list/1/10", { name: this.addBaseAddressForm.id ? this.addBaseAddressForm.parentName : this.pName }, "get")

            this.$refs.addBaseAddressRef.validate((valid) => {
                if (valid) {
                    let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                    params.append("name", this.addBaseAddressForm.name);
                    params.append("alias", this.addBaseAddressForm.alias);
                    params.append("campus", this.user.campus);
                    // this.id == "[object PointerEvent]" ? '' : params.append("parentId", this.id)
                    if (this.pName == '' || this.pName == undefined) {
                        params.append("parentName", response.list[0].name ? response.list[0].name : "");
                        params.append("parentId", response.list[0].id ? response.list[0].id : 0)
                    } else {
                        params.append("parentId", response.list[0].id)
                        params.append("parentName", response.list[0].name);
                    }

                    if (this.addBaseAddressForm.id) {
                        params.append("id", this.addBaseAddressForm.id);
                        httpService("/javaApi/baseAddress/edit", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success("修改成功");
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getBaseAddressList();
                                } else {
                                    ElMessage.error("修改失败");
                                }
                            }
                        );
                    } else {
                        httpService("/javaApi/baseAddress/add", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success("添加成功");
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getBaseAddressList();
                                } else {
                                    ElMessage.error("添加失败");
                                }
                            }
                        );
                    }
                } else {
                    return false;
                }
            });
        },
    },
};
</script>
<style>
.el-dialog__header {
    padding: var(--el-dialog-padding-primary);
    padding-bottom: 10px;
    border-bottom: 1px solid #dedede;
    margin-bottom: 15px;
}
</style>